<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Validate;


class Common extends Controller
{
    protected $req; //用来处理客户端传递过来的参数
    protected $validater; //用来验证数据/参数
    protected $params; //过滤后符合要求的参数

    //控制器下面方法所要接受参数的
    protected $rules = array(
        'User' => array(
            'adduser' => array(
                'userName' => ['require'],
                'eUserName' => ['require'],
                'institute' => ['require'],
                'subject' => ['require'],
                'researchDirection' => ['require'],
                'userDetail' => ['require'],
                //'openId' => ['require'],
                //'unionId' => ['require'],
                //'userNo' => ['require'],
                //'userImg' => ['require'],
            ),
            'getuser' => array(
                'unionId' => ['require'],
            ),
            'getuserbyuserno' => array(
                'userNo' => ['require'],
            ),
            'getuserbyid' => array(
                'id' => ['require'],
            ),
            'updateuser' => array(
                'userName' => ['require'],
                //'eUserName' => ['require'],
                'institute' => ['require'],
                //'subject' => ['require'],
                //'researchDirection' => ['require'],
                //'userDetail' => ['require'],
                //'openId' => ['require'],
                'unionId' => ['require'],
                //'phone' => ['require'],
                //'userImg' => ['require'],
                //'email' => ['require'],
                //'address' => ['require'],
            ),
            'updateuserbyuserno' => array(
                'userName' => ['require'],
                //'eUserName' => ['require'],
                'institute' => ['require'],
                //'subject' => ['require'],
                //'researchDirection' => ['require'],
                //'userDetail' => ['require'],
                'userNo' => ['require'],
                //'phone' => ['require'],
                //'userImg' => ['require'],
                //'email' => ['require'],
                //'address' => ['require'],
            ),
            'getuserpage' => array(
                'userId' => ['require'],
            ),
            'shareuser' => array(
                'userId' => ['require'],
            ),
            'activationuser' => array(
                'id' => ['require'],
                'scholarId' => ['require'],
            ),
            'deluser' => array(
                'id' => ['require'],
            ),
            'adduserproblem' => array(
                'id' => ['require'],
                'problemType' => ['require'],
                'problemContent' => ['require'],
                'imgs' => ['require'],
            ),
            'findscholarbyuserid' => array(
                'userId' => ['require'],
                'keyWords' => ['require'],
            )
        ),
        'UserBuddy' => array(
            'adduserbuddy' => array(
                'userId' => ['require'],
                'buddyUserId' => ['require'],
            ),
            'getuserbuddypage' => array(
                'userId' => ['require'],
            ),
            'deluserbuddypage' => array(
                'buddyId' => ['require'],
            ),
        ),
        'Resource' => array(
            'getnewresourcenum' => array(
                'userId' => ['require'],
            ),
            'completeresource' => array(
                'userId' => ['require'],
                'email' => ['require'],
                'content' => ['require'],
            ),
            'getnewresourcenumbyyear' => array(
                'userId' => ['require'],
                'year' => ['require'],
            ),
            'getnewresourcenumbytype' => array(
                'userId' => ['require'],
            ),
            'getnewresourcenumpage' => array(
                'userId' => ['require'],
                'type' => ['require'],
            ),
			'setresourcesearch' => array(
                'userId' => ['require'],
                'resourceId' => ['require'],
            ),
            'addnewresource' => array(
                'userId' => ['require'],
                'resourceIdList' => ['require'],
            ),
            'getuserresourcenum' => array(
                'userId' => ['require'],
            ),
            'getuserresourcepage' => array(
                'userId' => ['require'],
            ),
            'deluserresource' => array(
                'idList' => ['require'],
            ),
            'getresourcebyid' => array(
                'id' => ['require'],
            ),
        ),
        'Scholar' => array(
            'getscholarbycondition' => array(
                'scholarName' => ['require'],
                'phone' => ['require'],
            ),
        ),
        'OrgCreator' => array(
            'updateorgcreator' => array(
                'userId' => ['require'],
            ),
        ),
        'UserAcademicPortrait' => array(
            'searchuseracademicportrait' => array(
            ),
            'recommendforyou' => array(
            ),
            'latestnews' => array(
            ),
            'topresults' => array(
            ),
            'recommendkeywords' => array(
            ),
            'getscholarinfo' => array(
                'kid' => ['require'],
            ),
            'getachievementlevel' => array(
                'id' => ['require'],
                'isClaim' => ['require'],
            ),
            'getscholarresourcepage' => array(
                'id' => ['require'],
                'isClaim' => ['require'],
                'resourceCode' => ['require'],
            ),
            'getelementaryrelationship' => array(
                'id' => ['require'],
            ),
            'getscholarrecommend' => array(
                'kid' => ['require'],
                'theme' => ['require'],
            ),
            'getscholarresearchareas' => array(
                'userNo' => ['require'],
            ),
            'getscholarhighfrequencyword' => array(
                'userNo' => ['require'],
            ),
        ),
        'TechnologySituationPerceive' => array(
            'getstandardmanage' => array(
            ),
            'getrecentlyresultentrant' => array(
            ),
            'getpaperportraitoverview' => array(
            ),
            'getpaperportraitdetail' => array(
            ),
            'getintellectualpropertyportrait' => array(
            ),
            'getresultsawardportrait' => array(
            ),
            'getresearchprojectportrait' => array(
            ),
            'getbookstatistics' => array(
            ),
            'getcollegelist' => array(
            ),
            'getprofessionaltitlelist' => array(
            ),
        ),
        'FusionDataWarehouse' => array(
            'getteachersteam' => array(
            ),
            'getcooperatedistributed' => array(
            ),
            'getresearchaward' => array(
            ),
            'getscientificresearchresults' => array(
            ),
        ),
        'InstitutionalCooperationPortrait' => array(
            'getinstitutionalcooperation' => array(
            ),
            'getforeigncooperation' => array(
            ),
            'getcooperationinformation' => array(
                'creator' => ['require'],
            ),
        ),
        'ScholarResume' => array(
            'addscholarresume' => array(
                'kid' => ['require'],
                'yearFrom' => ['require'],
                'yearTo' => ['require'],
                'agency' => ['require'],
                'education' => ['require'],
                'email' => ['require'],
            ),
            'updatescholarresume' => array(
                'id' => ['require'],
                'yearFrom' => ['require'],
                'yearTo' => ['require'],
                'agency' => ['require'],
                'education' => ['require'],
                'email' => ['require'],
            ),
            'getscholarresume' => array(
                'kid' => ['require'],
            ),
            'delscholarresume'=> array(
                'id' => ['require'],
            ),
        ),
        'ResearchDirection' => array(
            'getresearchdirection' => array(
            ),
        ),
    );

    
    /**
     * 构造方法
     */
    protected function _initialize()
    {

        parent::_initialize();
        $this->req = Request::instance();
        //验证参数,返回成功过滤后的参数数组
        $this->params = $this->checkParams($this->req->param(true));
    }


    //检测客户端传递过来的其他参数（用户名，其他相关）
    /*
    param: $arr [除了time,token以外的其他参数]
    return:     [合格的参数数组]
     */
    protected function checkParams($arr)
    {

        //1.获取验证规则 (Array)
        $rule = $this->rules[$this->req->controller()][$this->req->action()];

        //2. 验证参数并且返回错误
        $this->validater = new Validate($rule);

        if (!$this->validater->check($arr)) {
            $this->returnMsg(9, $this->validater->getError());
        }

        //3. 如果正常，就通过验证

        return $arr;
    }



    //返回信息
    protected function returnMsg($code, $msg = '', $data = [], $totel = 1)
    {
        $return_data['code'] = $code;
        $return_data['totel'] = $totel;
        $return_data['msg'] = $msg;
        $return_data['data'] = $data;
        header('Content-Type:application/json; charset=utf-8');
        echo json_encode($return_data);die;
    }


}
